<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>移动的天屎</title>
    <style>
        img {
            position: absolute;
        }
    </style>
</head>

<body>
    <img src="../实践演示/images/下载.png" alt="">
    <script>
        var img=document.querySelector('img')
        img.addEventListener('mousedown',function(){
            document.addEventListener('mousemove', move )
            //mousemove只要移动鼠标就会就会触发这个事件
            //console.log(1);
            //核心原理：每次移动鼠标都会获得最新的鼠标坐标，把这个X和Y坐标作为图片的top和left的值就可以移动图片了
            function move(e){
                var x = e.pageX;
            var y = e.pageY;
            // console.log('x坐标是' + x, 'y坐标是' + y)
            img.style.left=x-40+'px'
            img.style.top=y-50+'px'
            }
          document.addEventListener('mouseup',function(){
              document.removeEventListener('mousemove',move)
          })

        })
        
    </script>
</body>

</html>